package org.example.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Demo3 {

    public static void main(String[] args) throws IOException {
        URL URL = new URL("http://www.hyycinfo.com:80/index.html?a=1&b=2#abc");
        System.out.println("URL.getHost() = " + URL.getHost());
        System.out.println("URL.getPort() = " + URL.getPort());
        System.out.println("URL.getPath() = " + URL.getPath());
        System.out.println("URL.getQuery() = " + URL.getQuery());
        System.out.println("URL.getRef() = " + URL.getRef());
        System.out.println("URL.getProtocol() = " + URL.getProtocol());

        URLConnection urlConnection = URL.openConnection();
        InputStream inputStream = urlConnection.getInputStream();

        // 字节流转为字符流
        // InputStreamReader 是字符流, 可以将字节流转为字符流
        // 资源流(构造函数参数是文件, 字符串) or 处理流(构造函数参数是另一个流)
        InputStreamReader inputStreamReader =
                new InputStreamReader(inputStream,"utf-8");
        char[] chars = new char[100];
        int len;
        while((len = inputStreamReader.read(chars)) != -1){
            System.out.print(new String(chars,0,len));
        }

//        byte[] bytes = new byte[100];
//        int len;
//        while((len = inputStream.read(bytes)) != -1){
//            System.out.print(new String(bytes,0,len));
//        }
        inputStream.close();
    }
}
/**
 *  1. URL: 统一资源定位符
 *  2. URLConnection: 统一资源连接
 *  3. InputStream: 输入流
 *  4. OutputStream: 输出流
 */
